/**
 * @name plugin prestrain/预加载 unit
 * @copyright	tcsos	inc
 * @author	周琪
 * @version	v1.0
 */
function prestrain() {
    var prestrain_obj = {};

    //依赖标签
    prestrain_obj.own = '';

    //数据
    prestrain_obj.data = {		
        //文件
        files:{},
        //当前位置
        num:0,
        //总长度
        len: 0,
        //加载完毕，跳转
        href: '',
        //加载速度
        sleep: 100		
    };

    prestrain_obj.unit = {
        //预加载开始
        start:function() {			
            //创建皮肤
            prestrain_obj.unit.create_skip();

            //创建标签
            prestrain_obj.unit.create_label();

            //加载
            prestrain_obj.unit.loader();
        },
        //创建标签
        create_label:function() {                        
            str = '<div style="width:100%; float:left;" id="plan_main"><div class="loading_tiao">' +
            '<div class="loading_tiao_l" id="plan_bar" style="width:0%;"></div>' +
            '<div style="position:absolute; right:-50px; top:5px; color:#000000;">'+
            '<span id="plan_cur">0%</span></div></div>' +	
            '<p>正在加载：<span id="load_file"></span></p></div>';
            prestrain_obj.own.append(str);
        },
        //创建皮肤
        create_skip:function() {
            common_fun_obj.append_style(config.path.plugin + 'prestrain/css.css?v=' + config.version);                        
        },
        //加载
        loader:function() {
            if(prestrain_obj.data.num > prestrain_obj.data.len - 1) {
                prestrain_obj.unit.end();
                return;
            }
            var _file = prestrain_obj.data.files[prestrain_obj.data.num];            

            //进度
            width = (100 / (prestrain_obj.data.len - 1)) * prestrain_obj.data.num;
            width = width > 100 ? 100 : width;
            $('#plan_bar').css('width', width + '%');
            
            //显示
            $('#plan_cur').text(parseInt(width) + '%');
            $('#load_file').text(_file);

            //加载CSS
            if(_file.match(/.css/)) {
                if(config.browse.ie) {
                    new Image().src = _file;
                } else {
                    obj = document.createElement('object');
                    obj.data = _file;
                    obj.width = 0;
                    obj.height = 0;                    
                    document.body.appendChild(obj);
                }

                prestrain_obj.data.num++;

                //定制执行加载
                setTimeout(prestrain_obj.unit.loader, prestrain_obj.data.sleep);
                return false;
            }

            //进行文件加载
            $.ajax({
                url:_file,
                complete:function() {                                                        					
                    prestrain_obj.data.num++;

                    //定制执行加载
                    setTimeout(prestrain_obj.unit.loader, prestrain_obj.data.sleep);
                }
            });
        },
        //结束
        end:function() {			            
            if(prestrain_obj.data.href == '') {
                $('#plan_main').remove();
                return;
            }
            $(document.body).fadeOut('slow', function(){
                $(document.body).css({
                    'width': '100%',
                    'overflow': 'hidden',
                    'scroll' : 'no'
                });							
                //document.body.scroll = 'no';
                //document.body.style.overflow = 'hidden';
                $(document.head).find('link[href*="login"]').remove();
                $(document.head).find('link[href*="prestrain"]').remove();
                $(document.body).html('');
                $(document.body).load(prestrain_obj.data.href, function(){
                    $(this).show();
                })
            });
        }
    }

    return {
        //初始化
        init:function(param) {			
            prestrain_obj.data.files = param.files;
            prestrain_obj.data.len = param.files.length;
            prestrain_obj.own = param.own;

            if (param.sleep != undefined) {
                prestrain_obj.data.sleep = param.sleep;
            }

            if (param.href != undefined) {
                prestrain_obj.data.href = param.href;
            }						

            //开始加载
            prestrain_obj.unit.start();
        }
    }
}